<script language="javascript">

var current_link = -1;
var default_backgrounds = new Array();
var default_colors = new Array();
var setup_defaults = false;
var go_next = '[go_next]';
var go_context = '[go_context]';
var allow_scroll = [allow_scroll];

function setupDefaults ()
{
	if (!setup_defaults) {
		setup_defaults = true;
		for (i = 0; i < document.links.length; i++) {
			default_colors[i] = document.links[i].style.color;
			default_backgrounds[i] = document.links[i].style.backgroundColor;
		}
	}
}

document.onkeypress = function (e) {
	setupDefaults();
	
	if ((e.keyCode == 175) || (e.keyCode == 63232)) {
		highlightPreviousLink();
		return false;
	}
	else if ((e.keyCode == 176) || (e.keyCode == 63233)) {
		highlightNextLink();
		return false;
	}
	else if ((e.keyCode == 177) || (e.keyCode == 63235)) {
		if (go_next.length > 0) {
			location.href = go_next + '?r=' + Math.random();
		}
		else {
			gotoCurrentLink();
		}
		return false;
	}
	else if (e.keyCode == 13) {
		gotoCurrentLink();
		return false;
	}
	else if ((e.keyCode == 178) || (e.keyCode == 63234)) {
		location.href = go_context + '?r=' + Math.random();
		return false;
	}
	else {
		return true;
	}
}

function resetHighlightLinks ()
{
	for (i = 0; i < document.links.length; i++) {
		proto = document.links[i].style.color = default_colors[i];
		proto = document.links[i].style.backgroundColor = default_backgrounds[i];
	}
}

function highlightCurrentLink ()
{
	resetHighlightLinks();
	document.links[current_link].style.color = 'black';
	document.links[current_link].style.backgroundColor = '#fff8d2';
	x = document.links[current_link].offsetTop;
	scroll_x = x - (window.innerHeight / 2);
	if ((scroll_x > 0) && allow_scroll) {
		window.scrollTo (0, scroll_x);
	}
}

function highlightPreviousLink ()
{
	current_link = current_link - 1;
	if (current_link < 0) {
		current_link = 0;
		if (allow_scroll) {
			window.scrollTo (0, 0);
		}
	}
	else {
		highlightCurrentLink();
	}
}

function highlightNextLink ()
{
	current_link = current_link + 1;
	if (current_link >= document.links.length) {
		current_link = current_link - 1;
	}
	else {
		highlightCurrentLink();
	}
}

function gotoCurrentLink ()
{
	if (current_link >= 0) {
		page = document.links[current_link].href;
		location.href = page;
	}
}

</script>
